package com.sct.content;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

import com.sct.utils.DateTimeUtils;

public class MonitorDataDao {
	private ContentResolver contentResolver;
	public void openDB(Context context) {
		try {
			contentResolver = context.getContentResolver();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert(MonDataDO monDataDO) {
		try {
			ContentValues values = new ContentValues();
			values.put(SCTData.MonitorDataDO.MNODE, monDataDO.getMdnode());
			values.put(SCTData.MonitorDataDO.MTYPE, monDataDO.getMdtype());
			values.put(SCTData.MonitorDataDO.MINTIME, monDataDO.getMdintime()==null ? DateTimeUtils.getDateTime(new Date()) : monDataDO.getMdintime());
			values.put(SCTData.MonitorDataDO.MDATA, monDataDO.getMddata());
			contentResolver.insert(SCTData.MonitorDataDO.WORD_CONTENT_URI, values);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteById(Integer mdId) {
		try {
			String selection = "mdid=?";
			String[] selectionArgs = new String[]{""+mdId};
			contentResolver.delete(SCTData.MonitorDataDO.DICT_CONTENT_URI, selection, selectionArgs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public MonDataDO getById(Integer mdId) {
		try {
			String[] projection = new String[]{SCTData.MonitorDataDO.MID,
												SCTData.MonitorDataDO.MNODE,
												SCTData.MonitorDataDO.MTYPE,
												SCTData.MonitorDataDO.MINTIME,
												SCTData.MonitorDataDO.MDATA};
			String selection = "mdid=?";
			String[] selectionArgs = new String[]{""+mdId};
			Cursor cursor = contentResolver.query(SCTData.MonitorDataDO.WORD_CONTENT_URI, projection, selection, selectionArgs, null);
			if(cursor == null || cursor.getCount() == 0)
				return null;
			cursor.moveToNext();
			MonDataDO monDataDO = new MonDataDO();
			monDataDO.setMdid(cursor.getInt(0));
			monDataDO.setMdnode(cursor.getString(1));
			monDataDO.setMdtype(cursor.getString(2));
			monDataDO.setMdintime(cursor.getString(3));
			monDataDO.setMddata(cursor.getInt(4));
			return monDataDO;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public List<MonDataDO> findAll(String node, String type) {
		List<MonDataDO> res = new ArrayList<MonDataDO>();
		try {
			String[] projection = new String[]{SCTData.MonitorDataDO.MID,
					SCTData.MonitorDataDO.MNODE,
					SCTData.MonitorDataDO.MTYPE,
					SCTData.MonitorDataDO.MINTIME,
					SCTData.MonitorDataDO.MDATA};
			String sortOrder = "mdintime asc";
			String selection = "mdnode=? and mdtype=?";
			String[] selectionArgs = new String[]{node, type};
			Cursor cursor = contentResolver.query(SCTData.MonitorDataDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				MonDataDO monDataDO = new MonDataDO();
				monDataDO.setMdid(cursor.getInt(0));
				monDataDO.setMdnode(cursor.getString(1));
				monDataDO.setMdtype(cursor.getString(2));
				monDataDO.setMdintime(cursor.getString(3));
				monDataDO.setMddata(cursor.getInt(4));
				res.add(monDataDO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public List<MonDataDO> findPage(int pagesize, int page, String node, String type) {
		List<MonDataDO> res = new ArrayList<MonDataDO>();
		try {
			String[] projection = new String[]{SCTData.MonitorDataDO.MID,
					SCTData.MonitorDataDO.MNODE,
					SCTData.MonitorDataDO.MTYPE,
					SCTData.MonitorDataDO.MINTIME,
					SCTData.MonitorDataDO.MDATA};
			String sortOrder = "intime desc";
			String limit = ""+pagesize+" offset " + (page*pagesize);
			String selection = "mdnode=? and mdtype=?";
			String[] selectionArgs = new String[]{node, type};
			//String limit = "1,0";
			Cursor cursor = contentResolver.query(SCTData.MonitorDataDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder+"|"+limit);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				MonDataDO monDataDO = new MonDataDO();
				monDataDO.setMdid(cursor.getInt(0));
				monDataDO.setMdnode(cursor.getString(1));
				monDataDO.setMdtype(cursor.getString(2));
				monDataDO.setMdintime(cursor.getString(3));
				monDataDO.setMddata(cursor.getInt(4));
				res.add(monDataDO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
}
